//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.ScientificWorkflow.UIDesigner.Monitor
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Resources;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;
    using System.Xml.Serialization;
    using Microsoft.Research.ScientificWorkflow.TridentUtilities;
    using Microsoft.Practices.Unity;

    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class MonitorSettingsWindow : Window
    {
        private MonitorSettings monitorSettings;

        private MonitorSettingService monitorSettingService;

        private MonitorSettings monitorSettingsClone;

        /// <summary>
        /// Constructor.
        /// </summary>
        public MonitorSettingsWindow(IUnityContainer container)
        {
            this.InitializeComponent();
            this.brdrHeader.MouseLeftButtonDown += (sender, e) => { this.DragMove(); };

            this.monitorSettingService = container.Resolve<MonitorSettingService>();

            this.monitorSettings = this.monitorSettingService.DeserializeSettings();

            // Create clone for monitor settings. 
            // Clone object will use to keep the UI level changes and orignal object will be used by running workflow. 
            // It will avoid the runtime setting changes.
            this.monitorSettingsClone = (MonitorSettings)this.monitorSettings.Clone();

            this.DataContext = this.monitorSettingsClone;

            this.Owner = Application.Current.MainWindow;

            this.Loaded += new RoutedEventHandler(MonitorSettingsWindow_Loaded);
        }

        /// <summary>
        /// Event handler for the browse button click event.
        /// </summary>
        /// <param name="sender">Source.</param>
        /// <param name="e">Event arguments.</param>
        private void BrowseButtonClick(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.SaveFileDialog saveFileDialog = new System.Windows.Forms.SaveFileDialog();

            saveFileDialog.RestoreDirectory = true;

            // Set the filter to the openfiledialogbox.
            saveFileDialog.Filter = UIDesignerCodeConstants.MONITORLOGFILEFILTER;

            // seting default file name
            saveFileDialog.FileName = "Monitor";

            // Set the title for the dialog box.
            saveFileDialog.Title = TridentResourceManager.GetString("SelectLogFileDialogHeader");

            if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                // Set the logpath to the file selected.
                this.monitorSettingsClone.LogFilePath = saveFileDialog.FileName;
            }
        }

        /// <summary>
        /// Event handler for the cancel button click.
        /// </summary>
        /// <param name="sender">Source.</param>
        /// <param name="e">Event arguments.</param>
        private void CancelButtonClick(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Event handler for save button click.
        /// </summary>
        /// <param name="sender">Source</param>
        /// <param name="e">event arguments.</param>
        private void SaveButtonClick(object sender, RoutedEventArgs e)
        {
            try
            {
                if (MonitoringCheck.IsChecked.GetValueOrDefault() && LoggingCheck.IsChecked.GetValueOrDefault() && (logFileText.Text.Trim().Length == 0))
                {
                    TridentMessageBox.ShowTridentErrorMessageBox(this, TridentResourceManager.GetString("InvalidLogfilePathMessage"));
                    browseButton.Focus();
                    return;
                }
                // Get back the setting changes and serialize it for next use.
                this.monitorSettings = this.monitorSettingsClone;
                this.monitorSettingService.SerializeSettings(this.monitorSettings);
                this.Close();
            }
            catch (TridentCustomException)
            {
                TridentMessageBox.ShowTridentErrorMessageBox(TridentResourceManager.GetString("MonitorSettingSaveFail"));
            }
        }

        /// <summary>
        /// Event handler for the textbox tooltip opening event.
        /// Restricts the tooltip when the text is empty.
        /// </summary>
        /// <param name="sender">Source.</param>
        /// <param name="e">Event Args.</param>
        private void LogFileTextToolTipOpening(object sender, ToolTipEventArgs e)
        {
            TextBox fileTextBox = sender as TextBox;
            if (fileTextBox != null)
            {
                if (string.IsNullOrEmpty(fileTextBox.Text))
                {
                    e.Handled = true;
                }
            }
        }

        /// <summary>
        /// Event handler for Enable Monitor Checked
        /// </summary>
        /// <param name="sender">object</param>
        /// <param name="e">RoutedEventArgs</param>
        private void EnableMonitorChecked(object sender, RoutedEventArgs e)
        {
            this.EnableDisableEventCheckBox(true);
        }

        /// <summary>
        /// Event handler for Logging UnChecked
        /// </summary>
        /// <param name="sender">object</param>
        /// <param name="e">RoutedEventArgs</param>
        private void EnableLoggingUnchecked(object sender, RoutedEventArgs e)
        {
            this.EnableDisableLoggingCheckBox(false);
        }

        /// <summary>
        /// Event handler for Enable Logging Checked
        /// </summary>
        /// <param name="sender">object</param>
        /// <param name="e">RoutedEventArgs</param>
        private void EnableLoggingChecked(object sender, RoutedEventArgs e)
        {
            this.EnableDisableLoggingCheckBox(true);
        }

        /// <summary>
        /// Event handler for Monitor Un Checked
        /// </summary>
        /// <param name="sender">object</param>
        /// <param name="e">RoutedEventArgs</param>
        private void EnableMonitorUnchecked(object sender, RoutedEventArgs e)
        {
            this.EnableDisableEventCheckBox(false);
            this.EnableDisableLoggingCheckBox(false);
        }

        /// <summary>
        /// Enable and Disables the Event Checkboxes
        /// </summary>
        /// <param name="isenable"></param>
        private void EnableDisableEventCheckBox(bool isenable)
        {
            events_Workflow.IsEnabled = isenable;
            events_Activity.IsEnabled = isenable;
            events_Trident.IsEnabled = isenable;
            LoggingCheck.IsEnabled = isenable;
            this.EnableDisableLoggingCheckBox((this.monitorSettingsClone.EnableMonitoring && this.monitorSettingsClone.EnableLogging));
        }

        /// <summary>
        /// Enable and Disables the Logging Checkboxes
        /// </summary>
        /// <param name="isenable"></param>
        private void EnableDisableLoggingCheckBox(bool isenable)
        {
            level_AllCheckBox.IsEnabled = isenable;
            this.browseButton.IsEnabled = isenable;
        }

        /// <summary>
        /// Event handler for Loaded event
        /// </summary>
        /// <param name="sender">object</param>
        /// <param name="e">RoutedEventArgs</param>
        private void MonitorSettingsWindow_Loaded(object sender, RoutedEventArgs e)
        {
            this.EnableDisableEventCheckBox(this.monitorSettingsClone.EnableMonitoring);
            this.EnableDisableLoggingCheckBox((this.monitorSettingsClone.EnableMonitoring && this.monitorSettingsClone.EnableLogging));
        }
    }
}